Skip to content

refactor(components): extract useResponseActions hook#60

Merged
saagpatel merged 1 commit into
masterfrom
codex/refactor/wave5-7-draft-response-actions
Apr 21, 2026
Merged

refactor(components): extract useResponseActions hook#60
saagpatel merged 1 commit into
masterfrom
codex/refactor/wave5-7-draft-response-actions

Conversation

@saagpatel
Copy link
Copy Markdown
Owner

What

Seven response-action callbacks move out of `DraftTab.tsx` into `useResponseActions`:

  • `handleCopyResponse` — includes the copy-override audit prompt
  • `handleExportResponse`
  • `handleCancel`
  • `handleResponseChange`
  • `handleApplyTemplate`
  • `handleSaveAsTemplate`
  • `handleTemplateModalSave`

The hook also takes ownership of `showTemplateModal` and `templateModalRating` state.

Shell reduction: 1551 → 1451 LOC (-100 on this PR).

Why

These callbacks cluster around the response text itself — copying it, exporting it, canceling in-flight generation, tracking edits, and saving it as a template. The template modal state belongs with the two template handlers that drive it. Extracting them out of the shell leaves the shell with orchestration-only concerns (handleClear, handleSuggestionApply, conversation-submit, keyboard handlers, tree toggles, view-mode toggles, the imperative handle wire-up, and the JSX tree).

How

  1. Create `useResponseActions.ts` with the 7 callbacks + 2 state pieces + `resetResponseActions`.
  2. Remove the 2 state declarations (`showTemplateModal`, `templateModalRating`) from the shell.
  3. Replace the 3 template/apply callbacks + the 4 copy/export/cancel/change callbacks with a single hook destructure at the appropriate ordering point (after `useDraftGeneration` so `setGenerating` is available).
  4. Update `handleClear` to call `resetResponseActions()` instead of touching the two setters individually.
  5. Drop now-unused imports: `invoke` from tauri-apps/api/core (moved into the hook), `calculateEditRatio` (only used by handleCopyResponse, now in the hook).
  6. Add `useResponseActions.test.ts` (7 tests) covering: direct copy with citations, copy-override prompt path, copy-override cancelled, export success, cancel-with-partial-streaming-text, response-change edit flag, save-as-template modal open.

Testing

  • `pnpm tsc --noEmit` — clean.
  • `pnpm test` — 234 pass (was 227; +7 new hook tests).
  • `pnpm ui:gate:static` — exit 0.

Risk / Notes

  • `DraftTabHandle` unchanged (`copyResponse`, `exportResponse`, `cancelGeneration` still point at the hook's exports).
  • Copy override audit behavior preserved exactly — `audit_response_copy_override` invocation, prompt text, logEvent payload shape.
  • Edit-flag behavior on `handleResponseChange` preserved (sets to `text !== originalResponse`).

Wave 5 status after this PR

  • DraftTab: 2573 → 1451 LOC (-1122, ~44% total reduction)
  • Tests: 184 → 234 (+50 direct Draft concern tests)
  • 12 hooks + 1 panel + 1 handle pin extracted
  • ~450 LOC from the 1000-target. One more concern (PR D: useDraftLifecycle for autosave + init + keyboard effects) remains in the roadmap.

Move copy / export / cancel / response-change / template-apply /
save-as-template / template-modal-save out of DraftTab.tsx into a
single hook. Hook owns showTemplateModal + templateModalRating state;
shell consumes showTemplateModal as a prop into WorkspaceDialogs.
handleClear now calls resetResponseActions() instead of touching
those setters individually.
@saagpatel saagpatel merged commit 9333ec8 into master Apr 21, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants